package Game;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

import resource.Keyboard;
import resource.Sound;

import Screens.EnableSound;
import Screens.Screen;

public class CanvasGame extends Canvas implements Runnable{
	
    
	private MIDletGame midletGame;
	private Screen screen;
	public static boolean running = true;
	public Player[] sound;
	private int volume;

	public CanvasGame(MIDletGame midletGame) {
	        Runtime.getRuntime().gc();
	        this.setFullScreenMode(true);
	        this.midletGame = midletGame;
	        volume = 0;
	        sound = new Player[Sound.SOUND_LENGHT];
	        load_sound();
	        screen = new EnableSound(this);
	        start();
	    }
	 
	public MIDletGame getMidletGame() {
		return midletGame;
	}

	public void setMidletGame(MIDletGame midletGame) {
		this.midletGame = midletGame;
	}

	public Screen getScreen() {
		return screen;
	}

	public void setScreen(Screen screen) {
		this.screen = screen;
	}

	
	public int getVolume() {
		return volume;
	}

	public void setVolume(int volume) {
		this.volume = volume;
	}

	public final void start() {
	        Thread runner = new Thread(this);
	        runner.setPriority(Thread.MAX_PRIORITY);
	        runner.start();
	    }
	 
	public void run() {
		// TODO Auto-generated method stub
		while (running) {
            long cycleStartTime = System.currentTimeMillis();
            long timeSinceStart = 0;
            repaint();                
            timeSinceStart = System.currentTimeMillis() - cycleStartTime;
            try {
            if(timeSinceStart < 100) {
                Thread.currentThread();
				Thread.sleep(100 - timeSinceStart);
            }
//                Thread.sleep(GAME_FRAME);
            } catch (InterruptedException ex) {
                //sendError("");
            }
        }
	}
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(255, 255, 255);
		Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
		g.setFont(font);
		screen.paint(g);
	}
	
	protected void keyPressed(int keyCode){
		screen.keyEvent(keyCode, Keyboard.KEY_EVENT_DOWN);
	}
	
	protected void keyReleased(int keyCode){
		screen.keyEvent(keyCode, Keyboard.KEY_EVENT_UP);
	}

	protected void keyRepeated(int keyCode) {
		// TODO Auto-generated method stub
		super.keyRepeated(keyCode);
		screen.keyEvent(keyCode, Keyboard.KEY_EVENT_DOWN);
	}
	
	public void load_sound(){
		InputStream is;
		for(int i = 0; i< Sound.SOUND_LENGHT; i++){
			is = getClass().getResourceAsStream(Sound.soundSource[i]);
			try {
				sound[i] = Manager.createPlayer(is, "audio/X-wav");
				sound[i].prefetch();
				//sound[i].prefetch();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MediaException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void un_load_sound(){
		for(int i = 0; i< Sound.SOUND_LENGHT; i++){
				sound[i].close();
				sound[i] = null;
		}
	}
}
